# -*-coding:utf8-*-

import numpy as np
import cv2

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

img = cv2.imread("test4.jpg")
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

faces=face_cascade.detectMultiScale(
	gray,
	scaleFactor = 1.15,
	minNeighbors = 10,
	minSize = (5,5),
	flags = cv2.CASCADE_SCALE_IMAGE,
)

if len(faces)>0:
	for faceRect in faces:
		
		x,y,w,h = faceRect
		
		cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2,8,0)
		roi_gray = gray[y:y+h,x:x+w]
		roi_color = img[y:y+h,x:x+w]

		faceMidX = faces[0][0]+(faces[0][2]/2)
		for i in range(26):
			for j in range(26):
				img[faces[0][1]+(faces[0][3]/2)-13+i,faces[0][0]+(faces[0][2]/2)-13+j] = [255,255,255]

pos = img.shape	
midX = 0
midX = pos[1]/2;

for i in range(50):
	for j in range(50):
		img[pos[0]/2-25+i,midX-25+j] = [0,0,255]



# faces[0][0] 为横向坐标
# 注：opencv库坐标x,y反向


if len(faces)==1:
	if faceMidX > midX:
		print 'err:' + str(faceMidX - midX)
		print 2;
	else:
		print 'err:' + str(midX - faceMidX)
		print 1;
else:
	print "no target or too many targets"

cv2.imshow("img",img)

cv2.waitKey(0)